package cn.edu.ctbu.websql.controller;

import cn.edu.ctbu.websql.domain.Stscore;
import cn.edu.ctbu.websql.helper.PageUtils;
import cn.edu.ctbu.websql.service.StscoreService;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Optional;

@Slf4j
@RestController
@RequestMapping(value = "/webapi/stscore")
public class StscoreRestController {
    @Autowired
    private StscoreService stscoreService;
    /**
     * 读取url：/webapi/students/list，显示学生列表
     * @return
     */
    @GetMapping("/list")
    public PageUtils GetAll(Integer limit, Integer page){
        Pageable pageable = PageRequest.of(page-1,limit);

        Page<Stscore> temStscore = stscoreService.getAll(pageable);
        PageUtils pageUtils = new PageUtils(temStscore.getTotalElements(),temStscore.getContent());
        return  pageUtils;
    }
    @GetMapping("/getdata/{id}")
    public Optional<Stscore> getData(@PathVariable Integer id){
        return stscoreService.getData(id);
    }
    @PutMapping("/update/{id}")
    public Stscore update(@PathVariable Integer id,@Valid Stscore newScore){
        // 在方法参数上使用 @Valid 注解，触发实体类的验证
        Stscore updateScore = stscoreService.update(id, newScore);
        return updateScore;
    }
    @GetMapping("/search/{no}")
    public List<Stscore> searchData(@PathVariable String no){
        return stscoreService.searchData(no);
    }

}
